/**
 * Created by Administrator on 2017/5/26 0026.
 */



$(function () {



    /*登录 ，注册 小图标切换效果*/
    $(".trigger>a").each(function (key, ele) {
        $(this).on("click", function () {
            //下划线
            aCode.text(randomNum());
            aCode1.text(randomNum());
            $(this).addClass("active")
                .siblings("a").removeClass("active");
            //登录，注册切换
            $("form").eq(key).css("display", "block")
                .siblings("form").css("display", "none");
        })
    });


    /*注册*/

    /*获得焦点时 边框变绿色*/
    $(".message>input").on("focus", function () {
        $(this).addClass("bd_green")
            .parent().siblings(".message").children("input").removeClass("bd_green");
        $(this).prev().addClass("bd_green_r")
            .parent().siblings(".message").children("span").removeClass("bd_green_r");
    });

    /*随机验证码*/
    var aCode1 = $(".authCode1");
    var aCode = $(".authCode");
    aCode.text(randomNum());
    aCode1.text(randomNum());
    aCode.on("click", function () {
        $(this).text(randomNum());
    });
    aCode1.on("click", function () {
        $(this).text(randomNum());
    });


    /*失去焦点时进行判断*/
    var booll = false;
    var bool2 = false;
    var bool3 = false;
    var bool4 = false;
    /*用户名--手机 号码*/
    $(".userName").on("keyup blur", function () {
        var reg = /^((13[0-9])|(14(5|7))|(15([0-3]|[5-9]))|(18[5-9]))\d{8}$/;
        if (reg.test($(this).val())) {
            $(this).next("p").removeClass("warning");
            booll = true;
        } else {
            $(this).next("p").addClass("warning");
            booll = false;
        }
    });

    /*验证码*/
    $(".userAuthCode").on("blur", function () {
        if ($(this).val() == $(".authCode").text()) {
            $(this).siblings("p").removeClass("warning");
            bool2 = true;
        } else {
            $(this).siblings("p").addClass("warning");
            bool2 = false;
        }
    });


    /*密码*/
    $(".userPwd").on("keyup blur", function () {
        var reg = /^.{8,20}$/;
        if (reg.test($(this).val())) {
            $(this).next("p").removeClass("warning");
            bool3 = true;
        } else {
            $(this).next("p").addClass("warning");
            bool3 = false;
        }
    });
    /*确认密码*/
    $(".anginPwd").on("blur", function () {
        if ($(this).val() == $(".userPwd").val()) {
            $(this).next("p").removeClass("warning");
            bool4 = true;
        } else {
            $(this).next("p").addClass("warning");
            bool4 = false;
        }
    });

    $(".checkbox").prop("checked") ? $("#btn_reg").css("background", "#e14958") : $("#btn_reg").css("background", "#ccc");

    //安全协议选中
    $(".checkbox").on("click", function () {
        $(".checkbox").prop("checked") ? $("#btn_reg").css("background", "#e14958") : $("#btn_reg").css("background", "#ccc");
        $(".text_arror").css("display", "none");
    });

    //点击注册按钮
    $("#btn_reg").on("click blur", function () {
        if ($(".checkbox").prop("checked")) {
            if (booll && bool2 && bool3 && bool4) {
                $.ajax({
                    url: "http://127.0.0.1/daling/reg.php",
                    type: "post",
                    data: $(".register").serialize(),
                    success: function (res) {
                        var regRbj = JSON.parse(res);
                        if (regRbj.status == 1) {
                            alert(regRbj.msg);
                            $.cookie('userName', $(".userName").val(), {expires: 7});
                            $.cookie('userPwd', $(".userPwd").val(), {expires: 7});
                            window.location.href = "../index.html";
                        } else {
                            alert(regRbj.msg);
                        }
                    }
                })
            }
        } else {
            $(".text_arror").css("display", "block");
        }
    });


    var uname = $.cookie("userName");
    var upwd = $.cookie("userPwd");
    if (uname && upwd) {
        $(".loginName").val(uname);
        $(".loginPwd").val(upwd);
    }


    //登录界面
    var bool10 = false;
    var bool11 = false;
    $(".loginName").on("keyup", function () {
        var reg = /^((13[0-9])|(14(5|7))|(15([0-3]|[5-9]))|(18[5-9]))\d{8}$/;
        if (reg.test($(this).val())) {
            $(this).next("p").removeClass("warning");
            bool10 = true;
        } else {
            $(this).next("p").addClass("warning");
            bool10 = false;
        }
    });

    /*验证码*/
    $(".loginCode").on("blur", function () {
        if ($(this).val() == $(".authCode1").text()) {
            $(this).siblings("p").removeClass("warning");
            bool11 = true;
        } else {
            $(this).siblings("p").addClass("warning");
            bool11 = false;
        }
    });


    //点击登录按钮
    $("#btn_login").on("click", function () {
        if (bool10 && bool11) {
            $.ajax({
                url: "http://127.0.0.1/daling/login.php",
                type: "post",
                data: {"userName": $(".loginName").val(), "userPwd": $(".loginPwd").val()},
                success: function (res) {
                    var loginObj = JSON.parse(res);
                    if (loginObj.status == 1) {
                        alert(loginObj.msg);
                        $.cookie('userName', $(".loginName").val(), {expires: 7});
                        $.cookie('userPwd', $(".loginPwd").val(), {expires: 7});
                        window.location.href = "../index.html";
                    } else {
                        alert(loginObj.msg);
                    }
                }
            })
        }
    });


    //请接收服务条款


});

/*随机数*/
function randomNum() {
    var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
    var arr1 = [];
    for (var i = 0; i < 4; i++) {
        arr1.push(arr[parseInt(Math.random() * arr.length)]);
    }
    var str = "" + arr1[0] + arr1[1] + arr1[2] + arr1[3];
    return str;
}


